//签字
$(function(){
	canReturn();
	init();
});
	var sessAppBase = sessionStorage.getItem("appBase");
	var sendData = JSON.parse(sessAppBase);
	var orderId = sendData.orderId;
	var appId = sendData.appId;
	var sign = sendData.sign;
	var preprintedNo="";
	var policyRef = "";
	//init后的appBase
	var initAppBase = '';
	function init(){
		var params={"appId":appId,"orderId":orderId,"sign":sign};
		padMarketing.ajaxNormal(
			'/pad_marketing/main/initSign.action',
			params,
			function(data){
				console.log(data);
				initAppBase = data.appBase;
				var appBase = data.appBase;
				$("#proposalUrl").val(appBase.proposalUrl);
				$("#otherSignDocumentsUrl").val(appBase.otherSignDocumentsUrl);
				preprintedNo = appBase.preprintedNo;
				policyRef = appBase.policyRef;
				//38个字只针对新型产品。其它产品则不需要签38个字
				var productGroup3 = appBase.productGroup3;
				if('PARTICIPATING'!=productGroup3 && 'UNIVERSAL'!=productGroup3 && 'UNIT LINK'!=productGroup3){
					$("#comment").hide();
				}
				//投被保人是同一个人时被保人签名不显示
				var cps = data.cps;
				for(var i in cps){
					var cp = cps[i];
					var cpNo = cp.cpNo;
					var rlToMins = cp.rlToMins;
					if('1'==cpNo && 'SLF'==rlToMins){
						$("#insured").hide();
						$("#bts").hide();
						break;
					}
				}
				//新型产品才会需要显示其它签名的内容
				var otherSignDocumentsUrl = appBase.otherSignDocumentsUrl;
				if(null==otherSignDocumentsUrl || ''==otherSignDocumentsUrl){
					$("#otherSign").hide();
					//获取最新高度
					if(myScroll) {
						myScroll.refresh();
					}
				}
				//长期险种所需签名资料无需签署
				if(appBase.productGroup5 != "LONG TERM"){
					$("#otherSign").hide();	
				}

				//获取最新高度
				if(myScroll) {
					myScroll.refresh();
				}
				//调壳初始化签字配置
				initSign(policyRef,'1');

			},
			function(data){
				$('#alertMsg_btn3').click(function(){
					window.location.href = '/pad_marketing/ordermanager/index.action';
				});
				//alert(JSON.stringify(data));
		//		alert('失败');
			}
		);
	}

	function gotoPdfShow(flag){
		var pdfurl="";
		if(flag==0)pdfurl = $("#proposalUrl").val();
		if(flag==1)pdfurl = $("#otherSignDocumentsUrl").val();
		goPdfShow(pdfurl,flag,'N');
	}
	

	function goBack(){
		var url = getArgsFromHref("url");
		if(url==''){
			history.go(-1);
		}else if(url.indexOf("/")>-1){
			window.location = "../"+url;
		}else{
			window.location = url;
		}
	}
	/*function finishSign(){
		doSign();
	}*/
	/**
	 * 签字完成
	 */
	function signComplete(){
		
		//签字--签署声明
		var comment  =$("#comment").attr("style");
		var sign0 = $("#sign0").attr("style");
		if('display: none;'!=comment && 'display: none;'!=sign0){
			alertMsg('请完成签署声明！');
			return;
		}
		
		//投保单--投保人签名
		var sign1 = $("#sign1").attr("style");
		if('display: none;'!=sign1 ){
			alertMsg('请完成投保单（投保人）签名！');
			return;
		}
		//校验投保单--投保人拍照
		if(''==src1){
			alertMsg('请完成投保单（投/被保险人）清晰的胸部以上正面照拍摄！');
			return;
		}
		
		//投保单--经纪人/营销人员签名
		var sign3 = $("#sign3").attr("style");
		if('display: none;'!=sign3){
			alertMsg('请完成投保单（经纪人/营销人员）签名！');
			return;
		}
		//校验投保单--经纪人/营销人员拍照
		if(''==src3){
			alertMsg('请完成投保单（经纪人/营销人员）清晰的胸部以上正面照拍摄！');
			return;
		}
		
		//投保单--经纪人/营销人员签名
		var insured = $("#insured").attr("style");
		var sign2 = $("#sign2").attr("style");
		if('display: none;'!=insured && 'display: none;'!=sign2){
			alertMsg('请完成投保单签名！');
			return;
		}
		//校验投保单--投保人拍照
		if('display: none;'!=insured && ''==src2){
			alertMsg('请完成投保单（投/被保险人）清晰的胸部以上正面照拍摄！');
			return;
		}
		
		
		//所需签名资料签名
		var otherSign = $("#otherSign").attr("style");
		var sign4 = $("#sign4").attr("style");
		var sign5 = $("#sign5").attr("style");
		
		//所需签名资料投保人签名
		if('display: none;'!=otherSign && ('display: none;'!=sign4)){
			alertMsg('请完成所需签名资料（投保人）签名！');
			return;
		}

		//所需签名资料投保人拍照
		if('display: none;'!=otherSign && (''==src4)){
			alertMsg('请完成所需签名资料（经纪人/营销人员）清晰的胸部以上正面照拍摄！ ');
			return;
		}
		
		//所需签名资料经纪人/营销人员签名
		if('display: none;'!=otherSign && ('display: none;'!=sign5)){
			alertMsg('请完成所需签名资料（经纪人/营销人员）签名！');
			return;
		}
		
		//所需签名资料经纪人/营销人员拍照
		if('display: none;'!=otherSign && (''==src5)){
			alertMsg('请完成所需签名资料（经纪人/营销人员）清晰的胸部以上正面照拍摄！ ');
			return;
		}
		$('a.btn').attr('onclick','');
		
		
		//调壳生成加密字符串
		finishSign("doSign");
		
		
	}
	
	/**
	 * 隐藏返回和上一步按钮
	 */
	function canReturn(){
		var url = getArgsFromHref("url");
		if(url==''){
			$("#return").hide();
			$("#goback").hide();
		}
	}
	
	function pdfRefresh(flag){
		var pdfurl="";
		if(flag==0)pdfurl = "http://portal.allianz.com.cn/pad_marketing/main/proposal325535PDF.action";
		if(flag==1)pdfurl = "http://portal.allianz.com.cn/pad_marketing/main/otherSignDocuments325535.action";
		refreshPDF(pdfurl);
	}
	
	/*function goSign1(){
		showSign("../html/img/16-2-1.png",'','','img1');
	}*/
	
	/**
	 * 壳回调函数，展示签字图片
	 * 
	 * @param src
	 * @param pdfNum
	 * @param siger
	 * @param imgNum
	 */
	function showSign(src,pdfNum,siger,imgNum){
		
		var num = imgNum.substr(3,1);
		$("#sign"+num).hide();
		var img=document.getElementById(imgNum);
		src = 'data:image/png;base64,' + src;
		img.src = src;
		if(myScroll)
			myScroll.refresh();
	}
	
	/**
	 * 调用后台整合pdf并更新订单状态为待拍照U
	 * @param pdf1
	 * @param pdf2
	 */
	function doSign(pdf1,pdf2){
		var pdfURL1 = $("#proposalUrl").val();
		var pdfURL2 = $("#otherSignDocumentsUrl").val();
		var photoList1 = [src1,src2,src3];
		var photoList2 = [src4,src5];
		var pdfMessageList = [];
		if(pdfURL1){
			pdfMessageList.push({"pdfType":"1151","pdfURL":pdfURL1,"encode":pdf1,"photoList":photoList1});
		}
		if(pdfURL2&&initAppBase.productGroup5=='LONG TERM'){
			pdfMessageList.push({"pdfType":"1169","pdfURL":pdfURL2,"encode":pdf2,"photoList":photoList2});
		}
		var params={"appId":appId,"orderId":orderId,"sign":sign,"insured":preprintedNo,"pdfMessageList":pdfMessageList,"policyRef":policyRef};
		padMarketing.ajaxNormal('bjcapdf.action',params,
				function(data){
					var flag = data.flag;
					if('Y'==flag){
						window.location="cardPhoto.action?signtoupload=1";
					}
					$('a.btn').attr('onclick','signComplete();');
				},function(data){
					$('a.btn').attr('onclick','signComplete();');
				},
				true,//是否异步
				true,//是否显示加载电波图
				'不用美图都那么帅，不过字该练练啦！'//电波图中的话术
		);
	}
	function test1(){
		var params=undefined;
		padMarketing.ajaxNormal('diffRequest.action',params,
				function(data){
					console.log('成功')
				},function(data){
					console.log('失败')
				},
				true,//是否异步
				true,//是否显示加载电波图
				'不用美图都那么帅，不过字该练练啦！'//电波图中的话术
		);
	}
	
	function demo()
	{
		setTimeout (function () {
			alert("test");
		}, 0);
	}
	/*function goSign(){
		showSignTxt("../html/img/16-2-1.png");
	}*/
	function showSignTxt(src){
//		alertMsg("1");
		$("#sign0").hide();
		var img=document.getElementById("img0");
		src = 'data:image/png;base64,' + src;			
		img.src = src;
		if(myScroll)
			myScroll.refresh();
//		window.setTimeout("goToWeixin('','','www.baidu.com','')", 4000);
	}

	/**
	 * 展示拍照头像
	 * 
	 * @param src
	 * @param imgNum
	 */
	var src1 = "";
	var src2 = "";
	var src3 = "";
	var src4 = "";
	var src5 = "";
	function showPhoto(src,imgNum){
		if('pho1'==imgNum)src1 = src;
		if('pho2'==imgNum)src2 = src;
		if('pho3'==imgNum)src3 = src;
		if('pho4'==imgNum)src4 = src;
		if('pho5'==imgNum)src5 = src;
		var img=document.getElementById(imgNum);
		src = 'data:image/png;base64,' + src;			
		img.src = src;
		if(myScroll)
			myScroll.refresh();
	}
